﻿Imports Microsoft.LightSwitch.Threading
Imports System.Windows.Controls
Imports DocumentToolkit.Client
Imports LightSwitchApplication.UserCode

Namespace LightSwitchApplication

    Public Class AnimalDetail
        Private _count As Integer

        Public Property Number() As Integer
            Get
                Return _count
            End Get
            Set(ByVal value As Integer)
                _count = value
            End Set
        End Property

        Private Sub Animal_Loaded(succeeded As Boolean)
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Animal)
        End Sub

        Private Sub Animal_Changed()
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Animal)
        End Sub

        Private Sub AnimalDetail_Saved()
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Animal)
        End Sub

        Private Sub MediaCollectionAddAndEditNew_CanExecute(ByRef result As Boolean)
            ' Write your code here.

        End Sub

        Private Sub MediaCollectionAddAndEditNew_Execute()
            ' Write your code here.
            'Dim file = SelectFile()
            'If Not file Is Nothing Then
            'Dim reader = DocumentReader.Create(File)
            'Using reader
            'Dim document = Me.MediaCollection.AddNew
            ''Me.MediaProperty.FileName = file.Name
            ''Me.MediaProperty.File = reader.ToBinary
            'document.FileName = File.Name
            'document.File = reader.ToBinary
            '
            'If reader.RequiresConversionToXps Then
            '' conversion needed, get the XPS binary
            'document.FileXps = reader.ToXpsBinary
            'End If
            'End Using
            'End If
            Dispatchers.Main.BeginInvoke(
                Sub()
                    ' select only .rar and .zip
                    'Dim Sfw As New Select_File_Window("rar|*.rar|zip|*.zip", 1)

                    ' select any file
                    Dim Sfw As New SelectFileWindow("Mutt Scout Images|*.pdf;*.xps;*.jpg;*.png", 1)
                    AddHandler Sfw.Closed,
                        Sub()
                            If Sfw.DialogResult = True AndAlso Sfw.DocumentStream IsNot Nothing Then
                                Me.Details.Dispatcher.BeginInvoke(
                                    Sub()
                                        Dim Doc = Me.MediaCollection.AddNew
                                        Doc.FileName = Sfw.SafeFileName
                                        'Doc.Extensao = Sfw.Extensao
                                        Doc.File = Sfw.Fd
                                        Me.MediaCollection.SelectedItem = Doc
                                    End Sub)
                            End If
                        End Sub
                    Sfw.Show()
                End Sub)

        End Sub
        Private Function SelectFile()
            Dim result As FileInfo = Nothing
            Dispatchers.Main.Invoke(
            Sub()
                Dim dlg = New OpenFileDialog()
                If dlg.ShowDialog = True Then
                    result = dlg.File
                End If
            End Sub)

            SelectFile = result
        End Function

        Private Sub MovementsAddAndEditNew_CanExecute(ByRef result As Boolean)
            ' Write your code here.

        End Sub

        Private Sub MovementsAddAndEditNew_Execute()
            ' Write your code here.
            Number = 1
            Dim newMovement As Movement = Me.Movements.AddNew()
            'Me.Animals.SelectedItem.Movements = newMovement
            Me.OpenModalWindow("MovementsDialog")
        End Sub

        Private Sub MovementsEditSelected_CanExecute(ByRef result As Boolean)
            ' Write your code here.

        End Sub

        Private Sub MovementsEditSelected_Execute()
            ' Write your code here.
            Number = Nothing
            Me.OpenModalWindow("MovementsDialog")
        End Sub

        Private Sub EditDialogOk_Execute()
            ' Write your code here.
            Me.CloseModalWindow("MovementsDialog")
            If Me.Movements.Count = 1 Then
                Me.Animal.ActiveMovementDate = Me.Movements.SelectedItem.MovementDate
                Me.Animal.ActiveMovementType = Me.Movements.SelectedItem.MovementType
                Me.Animal.ActiveMovementID = Me.Movements.SelectedItem
                Me.Animal.ActiveMovementPerson = Me.Movements.SelectedItem.Person
                Me.Animal.ActiveMovementLocation = Me.Movements.SelectedItem.Location
                Me.Animal.ActiveMovementShelter = Me.Movements.SelectedItem.Shelter
                Select Case Me.Movements.SelectedItem.MovementType.Id
                    Case 1
                        Me.Animal.Archived = 1
                        'MsgBox("archived=True case 1")
                    Case 2 'Foster
                        Me.Animal.Archived = 0
                    Case 3 'Transfer
                        Me.Animal.Archived = 1
                    Case 4 'Escaped
                        Me.Animal.Archived = 1
                    Case 5 'Reclaimed
                        Me.Animal.Archived = 1
                    Case 6 'Stolen
                        Me.Animal.Archived = 1
                    Case 7 'Released to the Wild
                        Me.Animal.Archived = 1
                    Case 8 'Retailer
                        Me.Animal.Archived = 0
                    Case 9 'Reservation
                        Me.Animal.Archived = 0
                    Case 10 'Canceled reservation
                        Me.Animal.Archived = 0
                    Case 11 'Trial Adoption
                        Me.Animal.Archived = 1
                    Case 12 'Permanent Foster
                        Me.Animal.Archived = 1
                    Case 14 'Returned
                        Me.Animal.Archived = 0
                    Case Else
                        Me.Animal.Archived = 1
                End Select
            Else
                For Each m In Me.Movements
                    If Me.Movements.SelectedItem.MovementDate > m.MovementDate Then
                        Me.Animal.ActiveMovementDate = Me.Movements.SelectedItem.MovementDate
                        Me.Animal.ActiveMovementType = Me.Movements.SelectedItem.MovementType
                        Me.Animal.ActiveMovementID = Me.Movements.SelectedItem
                        Me.Animal.ActiveMovementPerson = Me.Movements.SelectedItem.Person
                        Me.Animal.ActiveMovementLocation = Me.Movements.SelectedItem.Location
                        Me.Animal.ActiveMovementShelter = Me.Movements.SelectedItem.Shelter

                        Select Case Me.Movements.SelectedItem.MovementType.Id
                            Case 1
                                Me.Animal.Archived = 1
                                'MsgBox("archived=True case 1")
                            Case 2 'Foster
                                Me.Animal.Archived = 0
                            Case 3 'Transfer
                                Me.Animal.Archived = 1
                            Case 4 'Escaped
                                Me.Animal.Archived = 1
                            Case 5 'Reclaimed
                                Me.Animal.Archived = 1
                            Case 6 'Stolen
                                Me.Animal.Archived = 1
                            Case 7 'Released to the Wild
                                Me.Animal.Archived = 1
                            Case 8 'Retailer
                                Me.Animal.Archived = 0
                            Case 9 'Reservation
                                Me.Animal.Archived = 0
                            Case 10 'Canceled reservation
                                Me.Animal.Archived = 0
                            Case 11 'Trial Adoption
                                Me.Animal.Archived = 1
                            Case 12 'Permanent Foster
                                Me.Animal.Archived = 1
                            Case 14 'Returned
                                Me.Animal.Archived = 0
                            Case Else
                                Me.Animal.Archived = 1
                        End Select
                    End If
                Next
            End If

            Number = Nothing
        End Sub

        Private Sub EditDialogCancel_Execute()
            ' Write your code here.
            Me.CloseModalWindow("MovementsDialog")
            If Number = 1 Then
                Me.Movements.SelectedItem.Delete()
            End If
            Number = Nothing
        End Sub

        Private Sub AnimalDetail_Created()
            Dim Upload = Me.FindControl("Upload")
            AddHandler Upload.ControlAvailable, AddressOf Upload_ControlAvailable

        End Sub

        Private Sub Upload_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
            RemoveHandler Me.FindControl("Upload").ControlAvailable, AddressOf Upload_ControlAvailable
            Dim Button = DirectCast(e.Control, Button)
            AddHandler Button.Click, AddressOf Upload_Click

        End Sub

        Private Sub Upload_Click(sender As Object, e As System.Windows.RoutedEventArgs)
            Dim result As FileInfo = Nothing
            Dispatchers.Main.Invoke(
            Sub()
                Dim dlg = New OpenFileDialog()
                dlg.Filter = "Mutt Scout Images|*.pdf;*.xps;*.jpg;*.png"
                If dlg.ShowDialog = True Then
                    Dim file = dlg.File
                    If Not file Is Nothing Then
                        Me.Details.Dispatcher.BeginInvoke(
                            Sub()
                                'Dim reader = DocumentReader.Create(file)
                                'Using reader
                                'Dim document = Me.MediaCollection.AddNew
                                'document.FileName = file.Name
                                'document.File = reader.ToBinary

                                'If reader.RequiresConversionToXps Then
                                '' conversion needed, get the XPS binary
                                'document.FileXps = reader.ToXpsBinary
                                'End If
                                'End Using
                                'End Sub)

                                Dim bData As Byte()
                                Dim br As BinaryReader = New BinaryReader(file.OpenRead())
                                bData = br.ReadBytes(br.BaseStream.Length)
                                'Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)


                                Dim document = Me.MediaCollection.AddNew()
                                document.FileName = file.Name
                                document.File = bData
                                br.Close()
                            End Sub)
                    End If
                End If
            End Sub)

        End Sub

        Private Sub AnimalDetail_InitializeDataWorkspace(saveChangesTo As List(Of Microsoft.LightSwitch.IDataService))
            ' Write your code here.
            If Application.User.HasPermission(CanUpdateMedia) Then
                Me.FindControl("MediaCollection_DeleteSelected").IsVisible = True
                Me.FindControl("Upload").IsVisible = True
            Else
                Me.FindControl("MediaCollection_DeleteSelected").IsVisible = False
                Me.FindControl("Upload").IsVisible = False
            End If

            If Application.User.HasPermission(Permissions.CanUpdateMovements) Then
                Me.FindControl("MovementsAddAndEditNew").IsVisible = True
                Me.FindControl("MovementsEditSelected").IsVisible = True
                Me.FindControl("Movements_DeleteSelected").IsVisible = True
            Else
                Me.FindControl("MovementsAddAndEditNew").IsVisible = False
                Me.FindControl("MovementsEditSelected").IsVisible = False
                Me.FindControl("Movements_DeleteSelected").IsVisible = False
            End If

            If Application.User.HasPermission(Permissions.CanUpdateVaccines) Then
                Me.FindControl("Vaccines_AddAndEditNew").IsVisible = True
                Me.FindControl("Vaccines_EditSelected").IsVisible = True
                Me.FindControl("Vaccines_DeleteSelected").IsVisible = True
            Else
                Me.FindControl("Vaccines_AddAndEditNew").IsVisible = False
                Me.FindControl("Vaccines_EditSelected").IsVisible = False
                Me.FindControl("Vaccines_DeleteSelected").IsVisible = False
            End If

            If Application.User.HasPermission(Permissions.CanCreateLogs) Then
                Me.FindControl("Logs_AddAndEditNew").IsVisible = True
            Else
                Me.FindControl("Logs_AddAndEditNew").IsVisible = False
            End If

            If Application.User.HasPermission(Permissions.CanUpdateAnimals) Then
                Me.FindControl("Logs_AddAndEditNew").IsVisible = True
                Me.FindControl("Logs_EditSelected").IsVisible = True
                Me.FindControl("Logs_DeleteSelected").IsVisible = True
            Else
                Me.FindControl("Logs_EditSelected").IsVisible = False
                Me.FindControl("Logs_DeleteSelected").IsVisible = False
            End If
        End Sub
    End Class

End Namespace